/**********************************************************************
* File: start.cpp
* ID  : 
* Purpose: The command interface for IF-Engin.
* Date   : 2004/04/29
* Author : T.S.Yo
* Description:
*    This file contains methods to start, maintain, and end the engine.
**********************************************************************/

//---------------------------------------------------------------------
// start()
//---------------------------------------------------------------------
//    Start the ifEngine.
//---------------------------------------------------------------------
void ifEngine::start()
{
    string cmd;              

    // Print welcome message
    for ( int i = 0; i < 5; i++ )
        cout << "\n";
    cout << "###  ####     ####  #   #   ####  ###  #   #  ####\n";
    cout << " #   #        #     ##  #  #       #   ##  #  #   \n";
    cout << " #   ###  ### ####  # # #  # ###   #   # # #  ####\n";
    cout << " #   #        #     #  ##  #   #   #   #  ##  #   \n";
    cout << "###  #        ####  #   #   ###   ###  #   #  ####\n";
    cout << "\n";
    cout << "\n";
    cout << "You enter the IF world, enter 'h' for help.\n";
    look();

    // Start of the engine
    do
    {
        command(cmd);
        checkEvent(eventFlag);
        checkEnd(cmd);
    }
    while ( cmd != "Q"  &&  cmd != "q" );
}
//---------------------------------------------------------------------


//---------------------------------------------------------------------
// end()
//---------------------------------------------------------------------
//    End the ifEngine.
//---------------------------------------------------------------------
void ifEngine::end()
{
    for ( int i = 0; i < 2; i++ )
        cout << "\n";
    cout << "You leave the IF world\n";
    cout << endl;
    cout << endl;
    score();
    cout << endl;
    inventory();
    cout << endl;
    cout << "Thanks for playing.\nHave a nice day.\n";
}
//---------------------------------------------------------------------


//---------------------------------------------------------------------
// checkEvent(bool eventFlag)
//---------------------------------------------------------------------
//    Check if special event happen.
//---------------------------------------------------------------------
void ifEngine::checkEvent(bool (&eventFlag)[10])
{
    Item tmpItem;
    // Check for position of pearl
    if ( (eventFlag[0] == false) && (map1.checkItem(5,"pearl",tmpItem)) )
    {
        cout << "You hear a click when you put the pearl onto the shrin.\n";
        player.setScore(player.getScore() + 100);
        eventFlag[0] = true;
    }

    // Check for position of diamond
    if ( (eventFlag[1] == false) && (map1.checkItem(9,"diamond",tmpItem)) )
    {
        cout << "You hear a click when you put the diamond into the symbol of earth.\n";
        player.setScore(player.getScore() + 100);
        eventFlag[1] = true;
    }

    // Check for position of emerald
    if ( (eventFlag[2] == false) && (map1.checkItem(6,"emerald",tmpItem)) )
    {
        cout << "You hear a click when you put the emerald onto the small plate.\n";
        player.setScore(player.getScore() + 100);
        eventFlag[2] = true;
    }

    // Check for position of topaz
    if ( (eventFlag[3] == false) && (map1.checkItem(10,"topaz",tmpItem)) )
    {
        cout << "You hear a click when you put the topaz on the floor.\n";
        player.setScore(player.getScore() + 100);
        eventFlag[3] = true;
    }

    // Check for position of ruby
    if ( (eventFlag[4] == false) && (map1.checkItem(11,"ruby",tmpItem)) )
    {
        cout << "You hear a click when you throw the ruby into the box.\n";
        player.setScore(player.getScore() + 100);
        eventFlag[4] = true;
    }

    // Check for position of sapphire
    if ( (eventFlag[5] == false) && (map1.checkItem(7,"sapphire",tmpItem)) )
    {
        cout << "You hear a click when you put the sapphire into the bowl.\n";
        player.setScore(player.getScore() + 100);
        eventFlag[5] = true;
    }

    // Check for position of obsidian
    if ( (eventFlag[6] == false) && (map1.checkItem(8,"obsidian",tmpItem)) )
    {
        cout << "You hear a click when you put the obsidian into the hole.\n";
        player.setScore(player.getScore() + 100);
        eventFlag[6] = true;
    }

    // Check for position of amethyst
    if ( (eventFlag[7] == false) && (map1.checkItem(12,"amethyst",tmpItem)) )
    {
        cout << "You hear a click when you put the amethyst on the cube.\n";
        player.setScore(player.getScore() + 100);
        eventFlag[7] = true;
    }

    // If all jewel in correct position, open locked door
    if ( (eventFlag[8] == false) && (eventFlag[0] == true) &&
         (eventFlag[1] == true)  && (eventFlag[2] == true) &&
         (eventFlag[3] == true)  && (eventFlag[4] == true) &&
         (eventFlag[5] == true)  && (eventFlag[6] == true) &&
         (eventFlag[7] == true) )
    {
        cout << "You hear a loud sound from the secret chamber.\n";
        player.setScore(player.getScore() + 1000);
        map1.setExit(1,4,'w');
        eventFlag[8] = true;
    }
}
//---------------------------------------------------------------------


//---------------------------------------------------------------------
// checkEnd(string &cmd)
//---------------------------------------------------------------------
//    Check if end game criterion.
//---------------------------------------------------------------------
void ifEngine::checkEnd(string &cmd)
{
    if ( player.getCurLocation() == 4 )
    {
        for ( int i = 0; i < 2; i++ )
            cout << "\n";
        cout << "You ran out of that ancient maze!  Congratulations!\n";
        cmd = "q";
    }

}
//---------------------------------------------------------------------

